package com.heima.behavior.service.Impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dto.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

  @Autowired MongoTemplate mongoTemplate;
  @Autowired ApBehaviorEntryService apBehaviorEntryService;
  @Autowired RedisTemplate redisTemplate;

  @Override
  public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {

    // 检查参数
    boolean islike = false;
    boolean isunlike = false;
    boolean isfollow = false;
    boolean iscollection = false;

    ApUser user = AppThreadLocalUtils.getUser();
    if (user == null) {
      CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
    }

    // 查询对于得实体类
    ApBehaviorEntry apBehaviorEntry =
        apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
    if (apBehaviorEntry == null) {
      CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "没有对于的行为实体");
    }
    Query query =
        Query.query(
            Criteria.where("entryId")
                .is(apBehaviorEntry.getId())
                .and("articleId")
                .is(dto.getArticleId()));
    // 查询是否存在对于得行为
    // 是否收藏
    ApCollection collectionBehavior = mongoTemplate.findOne(query, ApCollection.class);
    if (collectionBehavior != null) {
      iscollection = true;
    }
    // 是否点赞
    ApLikesBehavior likesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
    if (likesBehavior != null) {
      islike = true;
    }
    // 是否不喜欢
    ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
    if (unlikesBehavior != null) {
        isunlike =true;
    }
    //是否关注，这里用到redies
      ZSetOperations zSetOperations = redisTemplate.opsForZSet();
    Double score = zSetOperations.score(UserRelationConstants.FOLLOW_LIST + user.getId(), dto.getAuthorApUserId().toString());
    if (score!=null){
      isfollow = true;
    }

    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("isfollow", isfollow);
    resultMap.put("islike", islike);
    resultMap.put("isunlike", isunlike);
    resultMap.put("iscollection", iscollection);
    return ResponseResult.okResult(resultMap);
  }
}
